package com.itheima.controller;



import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.service.OrderService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.JedisPool;

import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private JedisPool jedisPool;

    @Reference
    private OrderService orderService;

    @PostMapping("/submit")
    public Result submit(@RequestBody Map<String,String> map){

        //接收用户传过来的验证码
        String c_code = map.get("validateCode");
        //获取该用户在服务器端的验证码
        String s_code = jedisPool.getResource().get(map.get("telephone") + RedisMessageConstant.SENDTYPE_ORDER);
        //判断用户输入的验证码是否正确
       // if(c_code!=null && c_code.equalsIgnoreCase(s_code))
        if(!StringUtils.equalsIgnoreCase(c_code,s_code)){
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }else{

            try {
                map.put("orderType", Order.ORDERTYPE_WEIXIN);  //设置预约类型为微信
                //添加订单
                Result result = orderService.add(map);
                return result;
            }catch(Exception ex){
                return new Result(false,MessageConstant.ORDER_FAIL);
            }
        }




    }


    @GetMapping("/findById/{id}")
    public Result findByid(@PathVariable Integer id){

        try {
            Map map = orderService.findById(id);
            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }
    }
}
